﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Parameters
{
    public partial class AddacadmicYear : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Buffer = true;
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);

            if (!IsPostBack)
            {
                tbETime.Attributes.Add("readOnly", "true");
                tbSTime.Attributes.Add("readOnly", "true");
                tbETime.Attributes.Add("OnClick","c.showMoreDay=false;c.show(this);");
                tbSTime.Attributes.Add("OnClick","c.showMoreDay=false;c.show(this);");

                BindDDl();
                ddlYear.SelectedValue = DateTime.Now.Year.ToString();
            }
        }

        /// <summary>
        ///下拉框绑定，从字典里面取值
        ///下拉框绑定,取系统时间前后五年
        /// </summary>
        protected void BindDDl()
        {
            //int CurrentYear = DateTime.Now.Year;
            //for (int i =-5; i <=11; i++)
            //{
            //    int year = CurrentYear + i;
            //    ddlYear.Items.Add(new ListItem(year.ToString(),year.ToString()));
            //}
            //ddlYear.SelectedValue = CurrentYear.ToString();

            LabMS.BLL.Dictionary BDicitonary = new LabMS.BLL.Dictionary();
            ExtendBLL.Dictionary EDictionary = new ExtendBLL.Dictionary();
            ddlYear.DataSource=EDictionary.GetItemListByDClass("ruxuenianfen");
            ddlYear.DataValueField = "Code";
            ddlYear.DataTextField = "Name";
            ddlYear.DataBind();
        }



        /////对于学期： 1为春季，2为秋季

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(tbETime.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page,"","开学时间不能为空!");
                tbETime.Focus();
            }
            else if (string.IsNullOrEmpty(tbSTime.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "结束时间不能为空!");
                tbSTime.Focus();
            }
            #endregion
            else
            {
                DateTime ETime=DateTime.Parse(tbETime.Text.Trim());//结束时间
                DateTime STime=DateTime.Parse(tbSTime.Text.Trim());//开学时间
                int a = ETime.CompareTo(STime);
                if (a != 1)
                {
                    Common.JShelper.JSAlert(this.Page, "", "结束时间应该比开学时间晚!");
                    tbETime.Text = "";
                    tbETime.Focus();
                }
                else
                {
                    LabMS.Model.Semester MS = new LabMS.Model.Semester();
                    MS.ETime = DateTime.Parse(tbETime.Text.Trim());
                    MS.STime = DateTime.Parse(tbSTime.Text.Trim());
                    MS.Year = int.Parse(ddlYear.SelectedValue.ToString());
                    MS.MSemester = int.Parse(ddlAcadmic.SelectedValue.ToString());
                    LabMS.BLL.Semester BS = new LabMS.BLL.Semester();
                    int count = BS.Add(MS);
                    if (count > 0)
                    {
                        Common.JShelper.JSAlertAndRedirect(this.Page, "", "保存成功！", "academic-year.aspx");
                    }
                    else
                    {
                        Common.JShelper.JSAlert(this.Page, "", "保存失败！");
                    }
                }
            }
        }
    }
}
